package com.langfeiyes.pattern.memento.demo2;

import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * 备忘录管理者
 */
public class GitCaretaker {

    private LinkedHashMap<String, IVersonCtrlMemento> map = new LinkedHashMap<>();

    public void addMemento(IVersonCtrlMemento memento){
        if(memento != null){
            this.map.put(memento.getCommentId(), memento);
        }
    }

    public IVersonCtrlMemento retriveMemento(){
        //默认获取最后一个
        Iterator<Map.Entry<String, IVersonCtrlMemento>> iterator = map.entrySet().iterator();
        Map.Entry<String, IVersonCtrlMemento> tail = null;
        while (iterator.hasNext()) {
            tail = iterator.next();
        }
        return tail.getValue();
    }

    public IVersonCtrlMemento retriveMemento(String commentId){
        return map.get(commentId);
    }

    public void showLog(){
        System.out.println("---------查看所有提交日志------------");
        map.values().forEach(IVersonCtrlMemento::info);
    }
}
